package training.osms.presentation.category;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;

import training.osms.business.controller.CategoryController;

@ManagedBean
public class NewCategory {

	private CategoryPanel panel;
	private boolean categorySaved;
	
	public NewCategory(){
		initialize();
	}
	
	public String initialize(){
		panel = new CategoryPanel();
		this.categorySaved = false;
		return "newCategory";
	}
	
	public boolean isCategorySaved() {
		return categorySaved;
	}

	public void setCategorySaved(boolean categorySaved) {
		this.categorySaved = categorySaved;
	}
	
	public CategoryPanel getPanel() {
		return panel;
	}

	public void setPanel(CategoryPanel panel) {
		this.panel = panel;
	}

	public void saveCategory(){
		CategoryController controller = new CategoryController();
		controller.saveCategory(panel.getCategory());
		
		this.categorySaved = true;
		
		FacesMessage message = new FacesMessage();
		message.setSummary("A categoria foi salva com sucesso");
		message.setSeverity(FacesMessage.SEVERITY_INFO);
		FacesContext context = FacesContext.getCurrentInstance();
		context.addMessage(null, message);
	}
}
